import ajaxOnePsnData from "./ajaxOnePsnData";
import {deepClone, cacheTools} from 'nc-lightapp-front';

export default function getOnePsnData(orgVal, pk_psnjob, callback) {

    let postData = {
        pk_org: orgVal.refpk,
        func_type: 3,
        pk_psnjob
    };

    return ajaxOnePsnData(postData)
        .then((res) => {
            if (res.success) {
                const allpks = this.props.table.getAllTableData('ncc60071702list').rows.map(item => item.values.pk_psnjob.value);
                cacheTools.set('allpks', allpks);
                let meta = this.props.meta.getMeta();
                res.data.displayArea = [];
                res.data.areaCodeList.forEach(item => {
                    if (item === 'bd_psndoc') {
                        const index = res.data.allArea['bd_psndoc'].items.findIndex(item => item.attrcode === 'photo');
                        if (index > -1) res.data.allArea['bd_psndoc'].items.splice(index, 1);
                    }
                    meta[item] = res.data.allArea[item];
                    if (meta[item].moduletype === 'table') {
                        meta['gridrelation'][item] = {
                            destBrowseAreaCode: null,
                            destEditAreaCode: null,
                            srcAreaCode: item,
                            tabRelation: [item]
                        };
                    }
                    res.data.displayArea.push(meta[item]);
                });

                meta.hi_psnjob_history = deepClone(res.data.allArea.hi_psnjob);
                meta.hi_psnjob_history.items.find(item => item.attrcode === 'ismainjob').visible = true;
                meta.hi_psnjob_history_down = deepClone(meta.hi_psnjob_history);
                this.props.meta.setMeta(meta);
                callback && callback(res.data);
            }
        });
}
